<template>
    <div class="msitecontainer">
       <!--top-->
      <HeaderTop :title="address.name" >
        <router-link :span="8" slot="left" to="/search" class="mm">
          <i class="el-icon-search"></i>
        </router-link>
        <router-link :span="4" slot="right" :to="userInfo._id?'/userinfo':'/login'" class="mm">
          <span v-if="!userInfo._id">登录|注册</span>
          <span v-else>
             <i class="el-icon-user"></i>
          </span>
        </router-link>
      </HeaderTop>
      <mt-swipe :auto="0" v-if="categorys.length">
        <mt-swipe-item  v-for="(categorys,index) in categorysArr" :key="index">
          <div class="eight-frank">
            <ul class="list-items">
              <li class="col3" v-for="(category,index) in categorys" :key="index" >
                <div class="imgcontainer">
                  <a href="#"><img :src="imgBaseUrl+ category.image_url" alt=""></a>
                  <p>{{category.title}}</p>
              </div>
              </li>
              <li class="col3">

              </li>
            </ul>
          </div>
        </mt-swipe-item>
      </mt-swipe>

      <img src="./img/msite_back.svg" alt="" v-else>

      <!--商家列表-->
      <ShopList></ShopList>
    </div>
</template>

<script>
  import {mapState} from 'vuex'
  import HeaderTop from '../../components/HeaderTop/HeaderTop'
  import ShopList from '../../components/ShopList/ShopList'

    export default {
        name: "mainSite",
      data(){
        return{
          imgBaseUrl: 'https://fuss10.elemecdn.com'
        }
      },
      components:{
          HeaderTop,
          ShopList
      },
      mounted(){
          //获取食品分类
          this.$store.dispatch('getFoodCategorys')
          console.log(this.getFoodCategorys);
          //获取商家liebiao
        this.$store.dispatch('getShops')

      },
      computed:{
        ...mapState(['address','categorys','userInfo']),

        //根据categorys一维数组计算成二维数组，因为swiper有三页数据，最大是八
        categorysArr(){
          const {categorys} = this
            //准备一个空的二维数组
          const arr = []
          //小数组用来保存数据
          let minArr = []
          //遍历categorys
          categorys.forEach(c=>{
            //如果小数组装满了，就创建新的小数组
            if (minArr.length === 8){
              minArr = []
            }
            if (minArr.length === 0){
              //如果是空的，把小数组保存到大数组
              arr.push(minArr)
            }
            minArr.push(c)
          })
          return arr
        }
      }
    }
</script>

<style scoped>
  .msitecontainer{height: 300px}
  .topBckg{
    background-color: #02a774;
    color: white;
    height: 40px;
    text-align: center;
    line-height: 40px;
  }
.mint-swipe{height: 200px}
  .swipeColor{
    background-color: red;
    height: 30%;
  }

  .el-col-10{
    width: 23.66667%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .el-col-4{width: 28%}
  .eight-frank{
    width: 100%;
  }
  .list-items{
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
  }
  .list-items .col3{
    position: relative;
    display: block;
    width: 25%;
    height: 60px;
    text-decoration: none;
    list-style: none;
    vertical-align: bottom;
    margin: 3px 0;
  }
  .imgcontainer img{
    width: 30%;
    height: auto;
  }
  .imgcontainer{vertical-align:baseline;}
  .imgcontainer p{
    font-size: 9px;
    text-align: center;
    margin-bottom: 0;
    vertical-align: bottom;
    position: absolute;
    bottom: 0;
  }
 .mm{
   background-color: #02a774;
   width: 25%;
   color: white;
   height: 40px;
   text-align: center;
   line-height: 40px;
   text-decoration: none;
   padding:0;
   margin: 0;
 }
</style>
